package com.buer.train.api;

import com.buer.train.business.domain.TrainTypeDO;
import com.buer.train.business.domain.TrainingDO;
import com.buer.train.business.service.TrainTypeService;
import com.buer.train.business.service.TrainingService;
import com.buer.train.common.annotation.Auth;
import com.buer.train.common.eumn.ResultEnum;
import com.buer.train.common.holder.LoginUser;
import com.buer.train.common.holder.LoginUserHolder;
import com.buer.train.common.utils.BaseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/train")
@Api(tags = {"培训相关接口"})
public class TrainController {

    @Autowired
    private TrainingService trainingService;

    @Autowired
    private TrainTypeService trainTypeService;

    @RequestMapping(value = "/train/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("培训列表")
    @Auth(value = "train:train:list")
    public BaseResult trainList(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        PageInfo<TrainingDO> trainingList = trainingService.getTrainingList(params);
        Map map = new HashMap<>();
        map.put("list", trainingList.getList());
        map.put("total", trainingList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS, map);
    }

    @RequestMapping(value = "/train/getAllTrain", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取所有培训")
    @Auth
    public BaseResult getAllTrain(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        List<TrainingDO> trainingDOList = trainingService.list(params);
        return BaseResult.build(ResultEnum.SUCCESS,trainingDOList);
    }

    @RequestMapping(value = "/train/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("新增培训")
    @Auth(value = "train:train:add")
    public BaseResult trainAdd(@RequestBody TrainingDO training) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        training.setDeptId(loginUserHolder.getDeptId());
        training.setCreateId(loginUserHolder.getDeptId());
        training.setCreateTime(new Date());
        training.setUpdateTime(new Date());
        if(trainingService.save(training)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/train/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除培训")
    @Auth(value = "train:train:remove")
    public BaseResult trainRemove(@RequestBody Map<String, Object> params) {
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.FAILURE);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        if(trainingService.remove(id)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/train/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改培训")
    @Auth(value = "train:train:update")
    public BaseResult trainUpdate(@RequestBody TrainingDO training) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        TrainingDO trainingDO = trainingService.get(training.getId());
        trainingDO.setName(training.getName());
        trainingDO.setContent(training.getContent());
        trainingDO.setDeptId(loginUserHolder.getDeptId());
        trainingDO.setDesc(training.getDesc());
        trainingDO.setEligibility(training.getEligibility());
        trainingDO.setStartTime(training.getStartTime());
        trainingDO.setEndTime(training.getEndTime());
        trainingDO.setTypeId(training.getTypeId());
        trainingDO.setUpdateId(loginUserHolder.getDeptId());
        trainingDO.setUpdateName(training.getUpdateName());
        trainingDO.setUpdateTime(new Date());
        if(trainingService.update(training)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/train/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询培训")
    @Auth
    public BaseResult trainGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        TrainingDO trainingDO = trainingService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,trainingDO);
    }

    @RequestMapping(value = "/trainType/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("培训类型列表")
    @Auth(value = "train:trainType:list")
    public BaseResult trainTypeList(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        PageInfo<TrainTypeDO> trainTypeList = trainTypeService.getTrainTypeList(params);
        Map map = new HashMap<>();
        map.put("list", trainTypeList.getList());
        map.put("total", trainTypeList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS, map);
    }

    @RequestMapping(value = "/trainType/getAllTrainType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取所有培训类型")
    @Auth
    public BaseResult getAllTrainType(@RequestBody Map<String, Object> params) {
        LoginUser loginUserHolder = LoginUserHolder.getLoginUserHolder();
        params.put("depts",loginUserHolder.getNodeDepts());

        List<TrainTypeDO> trainTypeDOList = trainTypeService.list(params);
        return BaseResult.build(ResultEnum.SUCCESS,trainTypeDOList);
    }

    @RequestMapping(value = "/trainType/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("新增培训类型")
    @Auth(value = "train:trainType:add")
    public BaseResult trainTypeAdd(@RequestBody TrainTypeDO trainType) {

        trainType.setCreateTime(new Date());
        trainType.setUpdateTime(new Date());
        if(trainTypeService.save(trainType)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/trainType/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除培训类型")
    @Auth(value = "train:trainType:remove")
    public BaseResult trainTypeRemove(@RequestBody Map<String, Object> params) {
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.FAILURE);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        if(trainTypeService.remove(id)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/trainType/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改培训类型")
    @Auth(value = "train:trainType:update")
    public BaseResult teacherUpdate(@RequestBody TrainTypeDO trainType) {
        TrainTypeDO trainTypeDO = trainTypeService.get(trainType.getId());
        trainTypeDO.setName(trainType.getName());
        trainTypeDO.setDesc(trainType.getDesc());
        trainTypeDO.setUpdateTime(new Date());
        if(trainTypeService.update(trainType)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/trainType/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询培训类型")
    @Auth
    public BaseResult trainTypeGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        TrainTypeDO trainTypeDO = trainTypeService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,trainTypeDO);
    }
}
